//
//  GuideViewController.m
//  MyBlog
//
//  Created by NCS on 17/3/1.
//  Copyright © 2017年 YSL. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>

@end

@implementation GuideViewController

- (instancetype)initGuideViewWithFrame:(CGRect)frame data:(NSArray *)data {
    self = [super initWithFrame:frame];
    
    self.dataList = data;
    
    if (self) {
        [self initScrollView:frame];
        [self initPageController];
        [self initGuidePages];
    }
    return self;
}

// Default frame is screen's frame
- (instancetype)initGuideViewData:(NSArray *)data {
    self = [self initGuideViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) data:data];
    
    return self;
}

// Init scrollView
- (void)initScrollView:(CGRect)frame {
    self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
    [self.scrollView setContentSize:CGSizeMake(frame.size.width * self.dataList.count, frame.size.height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.canCancelContentTouches = YES;
    self.scrollView.userInteractionEnabled = YES;
    
    [self addSubview:self.scrollView];
}

// Init pageController
- (void)initPageController {
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 10)];
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.153 green:0.533 blue:0.796 alpha:1.0];
    self.pageControl.numberOfPages = self.dataList.count;
    
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:self.pageControl];
}

// Init pages
- (void)initGuidePages{
    for (int i = 0; i < self.dataList.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.image = [UIImage imageNamed:self.dataList[i]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.tag = i;
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePage:)];
        [imageView addGestureRecognizer:singleTap];
        
        imageView.userInteractionEnabled = YES;
        
        [self.scrollView addSubview:imageView];
    }
}

#pragma mark -- tap image
-(void)changePage:(UITapGestureRecognizer *)recognizer {
    CGFloat pageWidth = CGRectGetWidth(SCREEN_FRAME);
    CGPoint scrollPoint = CGPointMake(pageWidth * (recognizer.view.tag + 1), 0);
    [self.scrollView setContentOffset:scrollPoint animated:YES];
    
    if (recognizer.view.tag == self.dataList.count - 1) {
        [self dismiss];
        return;
    }
    
    self.pageControl.currentPage = ++recognizer.view.tag;
    
}

- (void)dismiss {
    NSLog(@"dismiss");
    [UIView animateWithDuration:0.35 animations:^(void) {
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat PageIndictor = self.scrollView.contentOffset.x / SCREEN_WIDTH;
    self.pageControl.currentPage = roundf(PageIndictor);
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

}

@end
